package com.topboya.linker.ui.presenter;


import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.FileBytaskIdAndProductIdReponse;
import com.topboya.linker.model.response.TaskFileResponse;
import com.topboya.linker.model.response.TaskProductResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.SeeTaskInfoAtView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SeeTaskInfoAtPresenter extends BasePresenter<SeeTaskInfoAtView> {

    private static final String TAG = "LoginAtPresenter-";

    public SeeTaskInfoAtPresenter(BaseActivity context) {
        super(context);
    }

    public void getTaskProductList(String id) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getTaskProductList(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(taskProductResponse -> {
                    String success = taskProductResponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<TaskProductResponse.Data> taskProductResponselist = taskProductResponse.getData();
                       getView().getTaskProductList(taskProductResponselist);

                    }
                }, this::loginError);
    }

    public void getTaskFile(String id) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getTaskFile(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(taskFileResponse -> {
                    String success = taskFileResponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<TaskFileResponse.Data> taskFileResponselist = taskFileResponse.getData();
                        getView().getTaskFile(taskFileResponselist);

                    }
                }, this::loginError);
    }


    public void getFeekPruductFileBytaskIdAndProductId(String id,String productId) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getFeekPruductFileBytaskIdAndProductId(id,productId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(fileBytaskIdAndProductIdReponse -> {
                    String success = fileBytaskIdAndProductIdReponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<FileBytaskIdAndProductIdReponse.FileList> fileBytaskIdAndProductlist = fileBytaskIdAndProductIdReponse.getData().getFileList();
                        getView().getFeekPruductFileBytaskIdAndProductId(fileBytaskIdAndProductlist);

                    }
                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

}
